package cn.com.callback


import cn.com.elite.EliteBean
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.json.JSONException

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @Title: apiHSTFollowupStatistics
 * @Description: 华盛，统计 跟进任务+今日完成 情况统计
 * 1.跟进/未跟进用户：分配到顾问名下 填写了跟进记录才算已跟进(这种不算时间维度 只算他名下的所有客户)
 * 2.今日已完成与未完成 ：当日下班前分配给他的所有用户 和前工作日下班后和假期分配的所有用户（周一的任务 就要从周五下班开始算 周末两天分配的也要加上,还是要按照6点切分）
 * @author: chieh
 * @date 2022-12-23 12:30
 *
 */

public class apiHSTFollowupStatistics implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiHSTFollowupStatistics.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

		String dataSourceID = "dataSource";
        try {
			
			List date_formatlist = eliteBean.chaxun_sql("select date_format(curdate(),'%w') AS week,DATE_FORMAT(NOW(), '%d') AS week2 from dual", "dataSource");
            def date_format = date_formatlist.get(0).get("week");//周几
			
			//默认差值0天
			def daydifference=0;
			//周一的任务 就要从周五下班开始算 周末两天分配的也要加上,还是要按照6点切分
			if(date_format=="1"){
				daydifference=3;
			}else{//否则就按前一天6点切分
				daydifference=1;
			}
            
            //先TRUNCATE
            def TRUNCATEresult = eliteBean.update("TRUNCATE table hst_followup_statistics","dataSource");
            log.debug("TRUNCATE：" + TRUNCATEresult);

            String[] insertSQL = new String[4];
			//-- 1 当天跟进数
			insertSQL[0] ="insert into hst_followup_statistics(HANDLEGROUP_ID,HANDLEBY_ID,Followupcount,Followuptype) SELECT e.HANDLEGROUP_ID,e.HANDLEBY_ID,count(DISTINCT e.customer_guid),1 FROM elitetask e left join hs_advisercallnature hasn on hasn.customer_guid=e.customer_guid and hasn.objective_guid=e.objective_guid and hasn.CREATEDBY=e.HANDLEBY_ID where 1=1   and e.MAP_ID='MY48ZX'   and hasn.advisercallnature_2 is not null and e.createddate>date_format(now()-interval " + daydifference + " day,'%Y-%m-%d 18:00:00')  and e.createddate<=date_format(now(),'%Y-%m-%d 17:59:59') and hasn.createddate>=date_format(now()-interval " + daydifference + " day,'%Y-%m-%d 18:00:00') and hasn.createddate<=date_format(now(),'%Y-%m-%d 17:59:59') group by  e.HANDLEGROUP_ID,e.HANDLEBY_ID"; 
			//-- 2 当天未跟进数
			insertSQL[1] ="insert into hst_followup_statistics(HANDLEGROUP_ID,HANDLEBY_ID,Followupcount,Followuptype) SELECT e.HANDLEGROUP_ID,e.HANDLEBY_ID,count(DISTINCT e.customer_guid),2 FROM elitetask e left join hs_advisercallnature hasn on hasn.customer_guid=e.customer_guid and hasn.objective_guid=e.objective_guid and hasn.CREATEDBY=e.HANDLEBY_ID and hasn.createddate>=date_format(now()-interval " + daydifference + " day,'%Y-%m-%d 18:00:00') and hasn.createddate<=date_format(now(),'%Y-%m-%d 17:59:59') where 1=1   and e.MAP_ID='MY48ZX'   and hasn.advisercallnature_2 is null and e.createddate>=date_format(now()-interval " + daydifference + " day,'%Y-%m-%d 18:00:00')  and e.createddate<=date_format(now(),'%Y-%m-%d 17:59:59')  group by  e.HANDLEGROUP_ID,e.HANDLEBY_ID"; 
			
			//-- 3 跟进用户数
			insertSQL[2] ="insert into hst_followup_statistics(HANDLEGROUP_ID,HANDLEBY_ID,Followupcount,Followuptype) SELECT e.HANDLEGROUP_ID,e.HANDLEBY_ID,count(DISTINCT e.customer_guid),3 FROM elitetask e left join hs_advisercallnature hasn on hasn.customer_guid=e.customer_guid and hasn.objective_guid=e.objective_guid and hasn.CREATEDBY=e.HANDLEBY_ID where 1=1   and e.MAP_ID='MY48ZX'  and e.HANDLEGROUP_ID  IS NOT NULL and e.HANDLEBY_ID IS NOT NULL and e.HANDLEGROUP_ID !='' and e.HANDLEBY_ID !='' and hasn.advisercallnature_2 is not null group by  e.HANDLEGROUP_ID,e.HANDLEBY_ID"; 
			//-- 4 未跟进用户数
			insertSQL[3] ="insert into hst_followup_statistics(HANDLEGROUP_ID,HANDLEBY_ID,Followupcount,Followuptype) SELECT e.HANDLEGROUP_ID,e.HANDLEBY_ID,count(DISTINCT e.customer_guid),4 FROM elitetask e left join hs_advisercallnature hasn on hasn.customer_guid=e.customer_guid and hasn.objective_guid=e.objective_guid and hasn.CREATEDBY=e.HANDLEBY_ID where 1=1   and e.MAP_ID='MY48ZX'  and e.HANDLEGROUP_ID  IS NOT NULL and e.HANDLEBY_ID IS NOT NULL and e.HANDLEGROUP_ID !='' and e.HANDLEBY_ID !='' and hasn.advisercallnature_2 is null  group by  e.HANDLEGROUP_ID,e.HANDLEBY_ID"; 
            
			int[] sqlResult = eliteBean.updateBatch(insertSQL, dataSourceID);
            log.debug("提交数据库操作SQL结果:" + sqlResult.toString());

            return "{\"code\":\"1\",\"message\":\"成功\",\"data\":{\"endabled\":\"统计跟进任务+今日完成情况成功\"}}";
        } catch (JSONException e) {
            log.error("处理异常，失败：" + e.getMessage());
        }
    }

}